home *** CD-ROM | disk | FTP | other *** search
/ HamCall (April 1991) / HAMCALL CD-ROM (Buckmaster)(April 1991).BIN / prgming / ctutor / chap06.txt < prev    next >
Text File  |  1990-10-14  |  9KB  |  236 lines

  1.  
  2.                                                      Chapter 6
  3.  
  4.                                             DEFINES AND MACROS
  5.  
  6.  
  7.  
  8. AIDS TO CLEAR PROGRAMMING
  9. ______________________________________________________________
  10.  
  11. Load and display the file named DEFINE.C for    ==============
  12. your first look at some defines and macros.        DEFINE.C
  13. Notice lines 2 through 5 of the program, each   ==============
  14. starting with the word "#define".  This is
  15. the way all defines and macros are defined.  Before the actual
  16. compilation starts, the compiler goes through a preprocessor
  17. pass to resolve all of the defines.  In the present case, it
  18. will find every place in the program where the combination
  19. "START" is found and it will simply replace it with the 0
  20. since that is the definition.  The compiler itself will never
  21. see the word "START", so as far as the compiler is concerned,
  22. the zeros were always there.  Note that if the string is found
  23. in a string constant or in a comment, it will not be changed.
  24.  
  25. It should be clear to you by now that putting the word "START"
  26. in your program instead of the numeral 0 is only a convenience
  27. to you and actually acts like a comment since the word "START"
  28. helps you to understand what the zero is used for.
  29.  
  30. In the case of a very small program, such as that before you,
  31. it doesn't really matter what you use.  If, however, you had
  32. a 2000 line program before you with 27 references to "START",
  33. it would be a completely different matter.  If you wanted to
  34. change all of the "START"s in the program to a new number, it
  35. would be simple to change the one #define, but difficult to
  36. find and change all of the references to it manually, and
  37. possibly disastrous if you missed one or two of the
  38. references.
  39.  
  40. In the same manner, the preprocessor will find all occurrences
  41. of the word "ENDING" and change them to 9, then the compiler
  42. will operate on the changed file with no knowledge that
  43. "ENDING" ever existed.
  44.  
  45. It is a fairly common practice in C programming to use all
  46. capital letters for a symbolic constant such as "START" and
  47. "ENDING" and use all lower case letters for variable names.
  48. You can use any method you choose since it is mostly a matter
  49. of personal taste.
  50.  
  51.  
  52. IS THIS REALLY USEFUL?
  53. ______________________________________________________________
  54.  
  55. When we get to the chapters discussing input and output, we
  56. will need an indicator to tell us when we reach the
  57. end-of-file of an input file.  Since different compilers use
  58.  
  59.                                                            6-1
  60.  
  61.                                 Chapter 6 - Defines and Macros
  62.  
  63. different numerical values for this, although most use either
  64. a zero or a minus 1, we will write the program with a "define"
  65. to define the EOF used by our particular compiler.  If at some
  66. later date, we change to a new compiler, it is a simple matter
  67. to change this one "define" to fix the entire program.  In
  68. most C compilers, the EOF is defined in the STDIO.H file.  You
  69. can observe this for yourself by listing the STDIO.H file that
  70. was supplied with your compiler.
  71.  
  72.  
  73. WHAT IS A MACRO?
  74. ______________________________________________________________
  75.  
  76. A macro is nothing more than another define, but since it is
  77. capable of at least appearing to perform some logical
  78. decisions or some math functions, it has a unique name.
  79. Consider line 4 of the program on your screen for an example
  80. of a macro.  In this case, anytime the preprocessor finds the
  81. word "MAX" followed by a group in parentheses, it expects to
  82. find two terms in the parentheses and will do a replacement
  83. of the terms into the second definition.  Thus the first term
  84. will replace every "A" in the second definition and the second
  85. term will replace every "B" in the second definition.  When
  86. line 13 of the program is reached, "index" will be substituted
  87. for every "A", and "count" will be substituted for every "B".
  88. Once again, it must be stated that string constants and
  89. comments will not be affected.  Remembering the cryptic
  90. construct we studied a couple of chapters ago will reveal that
  91. "mx" will receive the maximum value of "index" or "count".
  92. In like manner, the "MIN" macro will result in "mn" receiving
  93. the minimum value of "index" or "count".
  94.  
  95. The results are then printed out.  There are a lot of
  96. seemingly extra parentheses in the macro definition but they
  97. are not extra, they are essential.  We will discuss the extra
  98. parentheses in our next example program.  Be sure to compile
  99. and execute DEFINE.C before going on to the next program.
  100.  
  101.  
  102. LETS LOOK AT A WRONG MACRO
  103. ______________________________________________________________
  104.  
  105. Load the file named MACRO.C and display it on    =============
  106. your screen for a better look at a macro and        MACRO.C
  107. its use.  The second line defines a macro        =============
  108. named "WRONG" that appears to get the cube of
  109. "A", and indeed it does in some cases, but it fails miserably
  110. in others.  The second macro named "CUBE" actually does get
  111. the cube in all cases.
  112.  
  113. Consider the program itself where the CUBE of i+offset is
  114. calculated.  If i is 1, which it is the first time through,
  115. then we will be looking for the cube of 1+5 = 6, which will
  116. result in 216. When using "CUBE", we group the values like
  117.  
  118.                                                            6-2
  119.  
  120.                                 Chapter 6 - Defines and Macros
  121.  
  122. this, (1+5)*(1+5)*(1+5) = 6*6*6 = 216.  However, when we use
  123. WRONG, we group them as 1+5*1+5*1+5 = 1+5+5+5 = 16 which is
  124. a wrong answer.  The parentheses are therefore required to
  125. properly group the variables together.  It should be clear to
  126. you that either "CUBE" or "WRONG" would arrive at a correct
  127. answer for a single term replacement such as we did in the
  128. last program.  The correct values of the cube and the square
  129. of the numbers are printed out as well as the wrong values for
  130. your inspection.
  131.  
  132. In line 5 we define "ADD_WRONG" according to the above rules
  133. but we still have a problem when we try to use the macro in
  134. line 23 and 24.  In line 24 when we say we want the program
  135. to calculate 5*ADD_WRONG(i) with i = 1, we get the result 5*1
  136. + 1 which evaluates to 5 + 1 or 6, and this is most assuredly
  137. not what we had in mind.  We really wanted the result to be
  138. 5*(1 + 1) = 5*2 = 10 which is the answer we get when we use
  139. the macro named "ADD_RIGHT(i)", because of the extra
  140. parentheses in the definition given in line 6.  A little time
  141. spent studying the program and the result will be worth your
  142. effort in understanding how to use macros.
  143.  
  144. In order to prevent the above problems, most experienced C
  145. programmers include parentheses around each variable in a
  146. macro and additional parentheses around the entire expression.
  147.  
  148. The remainder of the program is simple and will be left to
  149. your inspection and understanding.
  150.  
  151.  
  152.  
  153. WHAT IS AN ENUMERATION VARIABLE?
  154. ______________________________________________________________
  155.  
  156. Load and display the program named ENUM.C for   ==============
  157. an example of how to use the "enum" type            ENUM.C
  158. variable.  Line 4 contains the first "enum"     ==============
  159. type variable named "result" which is a
  160. variable which can take on any of the values contained within
  161. the parentheses.  Actually the variable "result" is an "int"
  162. type variable but can be assigned any of the values defined
  163. for it.  The names within the parentheses are "int" type
  164. constants and can be used anywhere it is legal to use an "int"
  165. type constant.  The constant "win" is assigned the value of
  166. 0, "tie" the value 1, "bye" the value 2, etc.
  167.  
  168. In use, the variable "result" is used just like any "int"
  169. variable would be used as can be seen by its use in the
  170. program.  The "enum" type of variable is intended to be used
  171. by you, the programmer, as a coding aid since you can use a
  172. constant named "mon" for control structures rather than the
  173. meaningless (at least to you) value of 1.  Notice that "days"
  174. is assigned the values of days of the week in the remainder
  175. of the program.  If you were to use a "switch" statement, it
  176.  
  177.                                                            6-3
  178.  
  179.                                 Chapter 6 - Defines and Macros
  180.  
  181. would be much more meaningful to use the labels "sun", "mon",
  182. etc, rather than the more awkward 0, 1, 2, etc.
  183.  
  184.  
  185. PROGRAMMING EXERCISE
  186. ______________________________________________________________
  187.  
  188. 1.   Write a program to count from 7 to -5 by counting down.
  189.      Use #define statements to define the limits. (Hint, you
  190.      will need to use a decrementing variable in the third
  191.      part of the "for" loop control.
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.                                                            6-4
  235.  
  236.